﻿using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.Rendering.Universal;

namespace Learning
{
    public class XRayDrawHiddenPass : ScriptableRenderPass
    {
        private Material _material;

        private FilteringSettings _filtering;

        private List<ShaderTagId> _shaderTagIdList = new List<ShaderTagId>();
        
        private RenderStateBlock _renderStateBlock;
        
        public XRayDrawHiddenPass(Material material)
        {
            renderPassEvent = RenderPassEvent.AfterRenderingOpaques;
            _material = material;
            
            _filtering = new FilteringSettings(RenderQueueRange.opaque, LayerMask.GetMask("Character"));
            
            _shaderTagIdList.Add(new ShaderTagId("SRPDefaultUnlit"));
            _shaderTagIdList.Add(new ShaderTagId("UniversalForward"));
            _shaderTagIdList.Add(new ShaderTagId("UniversalForwardOnly"));
            
            _renderStateBlock = new RenderStateBlock(RenderStateMask.Nothing);
            _renderStateBlock.mask |= RenderStateMask.Depth;
            _renderStateBlock.depthState = new DepthState(false, CompareFunction.Greater);
        }

        public override void Execute(ScriptableRenderContext context, ref RenderingData renderingData)
        {
            DrawingSettings drawingSettings = CreateDrawingSettings(_shaderTagIdList, ref renderingData, renderingData.cameraData.defaultOpaqueSortFlags);
            drawingSettings.overrideMaterial = _material;
            // drawingSettings.overrideMaterialPassIndex = overrideMaterialPassIndex;
            // drawingSettings.overrideShader = overrideShader;
            // drawingSettings.overrideShaderPassIndex = overrideShaderPassIndex;
            
            context.DrawRenderers(renderingData.cullResults, ref drawingSettings, ref _filtering, ref _renderStateBlock);
        }
    }
}
